﻿//using BookManageSystem;
//using log4net.Core;
//using MagazineManage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace User_subsystem
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }
        //用户的账号和姓名
        public static string  id;
        public static string name;
        public static string status;

      //管理员登录的方法
      private void AdminLogin()
        {
            //获取账号和密码  -> 对比
            string  id = txtId.Text;
            string  pwd=txtPassword.Text;
            
            DaoAdmin dao= new DaoAdmin();
            dao.connect();
            string sql = $"select * from [UserInfor] where [UserID] = '{id}' and Password = '{pwd}'";
            SqlDataReader reader =dao.read (sql);
           
            if (reader.Read()==true)
            {
                //表内有该用户的信息
                sql= $"select [Status] from [UserInfor] where [UserID]='{id}'";
                reader = dao.read(sql);
                reader.Read();
                string x= reader[0].ToString();
                //判断该用户是真管理员还是用户
                if (x=="用户")
                {
                    //没有登录权限
                    MessageBox.Show("您不是管理员，请检查账号密码是否正确", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if(x=="管理员")//是管理员
                {
                    FormLogin.id = id;
                    sql = $"select [UserName] from [UserInfor] where [UserID]='{id}'";
                    reader = dao.read(sql);
                    reader.Read();
                    FormLogin.name = reader[0].ToString();
                    reader.Close();
                    dao.DaoClose();
                    
                    txtId.Text = "";
                    txtPassword.Text = "";
                    //转到管理员窗口
                    FormAdmin form=new FormAdmin();
                    form.ShowDialog();

                }
            }
            else
            {   
                MessageBox.Show("登录失败，请检查账号或密码是否正确", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void UserLogin()
        {
            //获取账号和密码  -> 对比
            string id = txtId.Text;
            string pwd = txtPassword.Text;


            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"select * from [UserInfor] where [UserID] = '{id}' and Password = '{pwd}'";
            SqlDataReader reader = dao.read(sql);
            if (reader.Read() == true)
            {
                //表内有该用户的信息
                sql = $"select [Status] from [UserInfor] where [UserID]='{id}'";
                reader = dao.read(sql);
                reader.Read();
                string x = reader[0].ToString();
                //判断该用户是真管理员还是用户
                if (x == "管理员")
                {
                    //没有登录权限
                    MessageBox.Show("您不是用户，请检查账号密码是否正确", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (x == "用户")//是用户
                {
                    FormLogin.id = id;
                    sql = $"select [UserName] from [UserInfor] where [UserID]='{id}'";
                    reader = dao.read(sql);
                    reader.Read();
                    FormLogin.name = reader[0].ToString();
                    reader.Close();
                    dao.DaoClose();

                    txtId.Text = "";
                    txtPassword.Text = "";
                    //转到用户窗口
                    FormUser form = new FormUser();
                    form.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("登录失败，请检查账号或密码是否正确", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
          if(DialogResult .Yes==  MessageBox.Show("确认退出吗？", "消息", MessageBoxButtons.YesNo , MessageBoxIcon.Question))
            {
                //退出
                this.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FormLogon form = new FormLogon();
            form.ShowDialog();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //判断文本框是否有内容
            if(txtId.Text =="" || txtPassword.Text == "")
            {
                MessageBox.Show("有空项，请检查后输入", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // 判断登录的对象
            if(rbtnAdmin.Checked==true)//管理员
            {
                AdminLogin();
            }
            if (rbtnUser.Checked == true)//用户
            {
                UserLogin();
            }
        

        }

        private void rbtnAdmin_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 读取用户1000，判断是否存在初始管理员
            DaoAdmin daoadmin = new DaoAdmin();
            daoadmin.connect();
            string sql = $"select [Status] from [UserInfor] where [UserID]='{1000}' ";
            SqlDataReader reader = daoadmin.read(sql);
            //没找到就插入初始管理员
            if (reader.Read() == false)
            {
                sql = $"insert into [UserInfor] values('1000','zhangsan','13048468862','河北省保定市','admin1000','管理员')";
                if (daoadmin.Execute(sql) == 0)
                {
                    //初始化失败
                    MessageBox.Show($"插入初始管理员失败,请检查代码", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            reader.Close();
            daoadmin.DaoClose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtId_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
